****************************
**** Urgent Care in YTS ****
****************************

clear
set more off

********************************************************************************
// UCCs
********************************************************************************

use "YTS.dta", clear 
tostring sic, replace
gen sic_2d = substr(sic,1,2)
gen health_sic = (sic_2d=="80")
egen sic_max = max(health_sic), by(abi)
keep if sic_max==1
save "SIC80.dta", replace

* Search for keyword in entire YTS

use "SIC80.dta", clear
drop if company==""
ren longitude Longitude
ren latitude Latitude
replace Longitude = -1*Longitude if Longitude>0
replace Latitude = -1*Latitude if Latitude<0
merge m:1 zipcode using "ZiptoZCTA"
keep if _merge==3
drop _merge
keep if regexm(company, "URGENT")
duplicates drop Longitude Latitude year, force
save "AdditionalUCCs_R3.dta", replace

use "AdditionalUCCs_R3.dta", clear
ren abi eabi
ren Latitude eLatitude
ren Longitude eLongitude
ren company eog_company
tempfile base1
save `base1'

use "AdditionalUCCs_R3.dta", clear
tempfile base0
save `base0'

forvalues y=2005/2017{
	use `base1', clear
	keep if year==`y'
	keep eabi eLatitude eLongitude eog_company
	tempfile yearlybase1
	save `yearlybase1'
		
	use `base0', clear
	keep if year==`y'
	keep abi Latitude Longitude company
	tempfile yearlybase0
	save `yearlybase0'
	if(_N!=0){
		capture quietly{
			geonear abi Latitude Longitude using `yearlybase1', n(eabi eLatitude eLongitude) long ignoreself within(0.1)
			merge m:1 eabi using `yearlybase1'
			keep if _merge==3
			drop _merge
			ren abi abi1
			ren eabi abi2
			gen delete_eabi = abi2 if km_to_eabi<=0.1
			if(_N!=0){
				keep delete_eabi
				duplicates drop delete_eabi, force
				ren delete_eabi abi
				gen year=`y'
				duplicates drop abi year, force
				save "MinDist_add_`y'.dta", replace
			}
		}
	}
}

use "AdditionalUCCs_R3.dta", clear
duplicates drop abi year, force
gen notdrop = (regexm(company, "ALBANY URGENT CARE") | regexm(company, "SUPERIOR CLINIC URGENT CARE") | regexm(company, "MARINER MEDICAL CLINIC URGENT") | regexm(company, "DENFELD URGENT CARE") | regexm(company, "DULUTH CLINIC URGENT CARE") | regexm(company, "MILLER CREEK URGENT CARE") | regexm(company, "ESSENTIA HEALTH URGENT CARE") | regexm(company, "LAREDO URGENT CARE") | regexm(company, "LEVEL II URGENT CARE") | regexm(company, "RIO GRANDE CITY URGENT CARE") | regexm(company, "ZAPATA REGIONAL URGENT CARE") | regexm(company, "COMMUNITYMED FAMILY URGENT") | regexm(company, "MOSES LAKE CLINIC URGENT CARE") | regexm(company, "GILLETTE URGENT CARE"))
forvalues y=2005/2017{
	capture confirm file "MinDist_add_`y'.dta"
	  if _rc==0 {
		merge 1:1 abi year using "MinDist_add_`y'.dta"
		drop if _merge==3 & notdrop==0
		drop _merge
	  }		
}
duplicates drop abi year, force
drop notdrop
drop if abi==.
drop if company==""
cap drop ucc
save "AdditionalUCCs_R3f.dta", replace

use "HospitalNames2015.dta", clear
keep zipcode 
duplicates drop
save "hosp_zip.dta", replace

* Match remaining SolvHealth operators

use "SIC80.dta", clear
drop if company==""
ren longitude Longitude
ren latitude Latitude
replace Longitude = -1*Longitude if Longitude>0
replace Latitude = -1*Latitude if Latitude<0
gen og_company = company
replace company = strupper(company)
replace company = subinstr(company,"&","",.)
replace company = subinstr(company,"/","",.)
replace company = subinstr(company,"-","",.)
replace company = subinstr(company,"'","",.)
replace company = subinstr(company,".","",.)
replace company = subinstr(company,",","",.)
replace company = subinstr(company," ","",.)
save "master.dta", replace

qui forvalues y=2005/2017{
	use "master.dta", clear
	keep if year==`y'
	do "YTS_UrgentCare_P1_V2.do"
	gen is_hospital=(regexm(company, "HOSPITAL")|regexm( company, "MEDICALCENTER" ) | regexm(company, "CENTRO MEDICO") | regexm(company, "HEALTHCARESYSTEM") | regexm(company, "HEALTHCENTER")| regexm(company, "MEDCENTER") | regexm(company, "HEALTHSYSTEM") | regexm(company, "MEDICALCTR") | regexm(company, "MERCY") | regexm(company, "ESSENTIA") | regexm(company, "CARECENTER") | regexm(company, "UPMC") | regexm(company, "CAROLINAS") | regexm(company, "HOSP") |regexm(company, "VINCENT")|regexm(company, "LUKE")|regexm(company, "FRANCISCAN")|regexm(company, "MERIT")|regexm(company, "TENNOVA")|regexm(company, "BAPTIST")| regexm(company, "MCLAREN")|regexm(company, "TRINITY")|regexm(company, "WHEATON")|regexm(company, "MAYO")|regexm(company, "JOSEPH")|regexm(company, "SURGERY")|regexm(company, "ALLIANCE")|regexm(company, "ABRAZO")|regexm(company, "STCHARLES")|regexm(company, "BAYLOR")|regexm(company, "CLEVELANDCLINIC")|regexm(company, "UWHEALTH")|regexm(company, "LAKESREGIONAL")|regexm(company, "SUMMIT")|regexm(company, "CHIHEALTH")|regexm(company, "MEDCENTER")|regexm(company, "STFRANCIS")|regexm(company, "SGMC")|regexm(company, "AUGUSTAHEALTH")|regexm(company, "ANMEDHEALTH")|regexm(company, "STELIZABETH")|(regexm(company, "MEMORIAL") & regexm(company, "HEALTH"))|regexm(company, "CENTRACARE")|regexm(company, "ADVENTIST")|regexm(company, "STJAMES")|regexm(company, "GOODSAMARITAN")|regexm(company, "STJOHN")|regexm(company, "STEDWARD")|regexm(company, "SURGICAL")|regexm(company, "TEXASHEALTH")|regexm(company, "ETMC")|regexm(company, "CENTURAHEALTH")|regexm(company, "PROVIDENCELITTLE")|(regexm(company, "JEFFERSON") & regexm(company, "HEALTH")))
	merge m:1 zipcode using "hosp_zip"
	drop if _merge==2
	replace is_hospital = 0 if _merge==1 & is_hospital==1
	drop _merge
	egen ucc = rowtotal(is_amfam-is_hospital)
	replace ucc = ucc - is_hospital
	keep if ucc>0
	qui ds 
	foreach v of varlist `r(varlist)'{
		label var `v' "`v'"
	}
	qui ds, has(varl *is_*)
	local vars `r(varlist)'
	local not is_hospital
	local is_varlist: list vars - not
	foreach op of varlist `is_varlist'{
		replace `op' = -1 if is_hospital==1 & `op'!=0 
	} 
	save "UCC_`y'_R2.dta", replace
}

use "UCC_2005_R2.dta", clear
forvalues y = 2006/2017{
	append using "UCC_`y'_R2.dta", force
}
drop ucc
egen ucc = rowtotal(is_amfam-is_bruises)
drop if ucc==0
duplicates drop Longitude Latitude year, force
drop if Longitude<-180 | Longitude>180
drop if Latitude<-90 | Latitude>90
ren is_hospital a_hospital
drop if abi==.
drop if company==""
save "RawUrgentCare_R3.dta", replace

* Drop UCCs that are within 100 meters of each other

use "RawUrgentCare_R3.dta", clear
keep if ucc>0
append using "AdditionalUCCs_R3f.dta", force
duplicates drop abi year, force 
duplicates drop Longitude Latitude year, force 
drop if company==""
drop if abi==.
tempfile operator
save `operator'

use `operator', clear
ren abi eabi
ren Latitude eLatitude
ren Longitude eLongitude
ren og_company eog_company
tempfile base1
save `base1'

use `operator', clear
tempfile base0
save `base0'

forvalues y=2005/2017{
	use `base1', clear
	keep if year==`y'
	keep eabi eLatitude eLongitude eog_company
	tempfile yearlybase1
	save `yearlybase1'
		
	use `base0', clear
	keep if year==`y'
	keep abi Latitude Longitude og_company
	tempfile yearlybase0
	save `yearlybase0'
	if(_N!=0){
		capture quietly{
			geonear abi Latitude Longitude using `yearlybase1', n(eabi eLatitude eLongitude) long ignoreself within(0.1)
			merge m:1 eabi using `yearlybase1'
			keep if _merge==3
			drop _merge
			ren abi abi1
			ren eabi abi2
			gen delete_eabi = abi2 if km_to_eabi<=0.1
			if(_N!=0){
				keep delete_eabi
				duplicates drop delete_eabi, force
				ren delete_eabi abi
				gen year=`y'
				duplicates drop abi year, force
				save "MinDist_ucc_`y'.dta", replace
			}
		}
	}
}

use "RawUrgentCare_R3.dta", clear
forvalues y=2005/2017{
	capture confirm file "MinDist_ucc_`y'.dta"
	  if _rc==0 {
		merge 1:1 abi year using "MinDist_ucc_`y'.dta"
		drop if _merge==3
		drop _merge
	  }		
}
duplicates drop Longitude Latitude year, force
drop if abi==.
drop if company==""
drop if is_hospital==1
drop if a_hospital==1
gen todrop = 0

replace todrop = 1 if regexm(company, "CHIRO")
replace todrop = 1 if regexm(company, "REHAB")
replace todrop = 1 if regexm(company, "OBSTE")
replace todrop = 1 if regexm(company, "GYNEC")
replace todrop = 1 if regexm(company, "ORTH")
replace todrop = 1 if regexm(company, "OPTIC")
replace todrop = 1 if regexm(company, "CARDIOLOGY")
replace todrop = 1 if regexm(company, "MEDICALSPA")
replace todrop = 1 if regexm(company, "VASCULAR")
replace todrop = 1 if regexm(company, "ENDCRNLGY")
replace todrop = 1 if regexm(company, "DIAGNOSTIC")
replace todrop = 1 if regexm(company, "STAFFING")
replace todrop = 1 if regexm(company, "MASSAGE")
replace todrop = 1 if regexm(company, "IMAGING")
replace todrop = 1 if regexm(company, "OBGYN")
replace todrop = 1 if regexm(company, "LABORATORY")
replace todrop = 1 if regexm(company, "DENTAL")
replace todrop = 1 if regexm(company, "CANCER")
replace todrop = 1 if regexm(company, "DEVICES")
replace todrop = 1 if regexm(company, "AESTHETICS")
replace todrop = 1 if regexm(company, "PALLIATIVE")
replace todrop = 1 if regexm(company, "EQUIPMENT")
replace todrop = 1 if regexm(company, "HOSPITAL")
replace todrop = 1 if regexm(company, "MEDICAL")
replace todrop = 1 if regexm(company, "CEREBRAL")
replace todrop = 1 if regexm(company, "MEDCTR")
replace todrop = 1 if regexm(company, "SURG")
replace todrop = 1 if regexm(company, "SURGERY")
replace todrop = 1 if regexm(company, "HOME")
replace todrop = 1 if regexm(company, "NEURO")
replace todrop = 1 if regexm(company, "OCEAN")
replace todrop = 1 if regexm(company, "NURSING")
replace todrop = 1 if regexm(company, "PEDIAT")
replace todrop = 1 if regexm(company, "EYE")
replace todrop = 1 if regexm(company, "ONCO")
replace todrop = 1 if regexm(company, "WALMART")
replace todrop = 1 if regexm(company, "ORTHO")
replace todrop = 1 if regexm(company, "DIABE")
replace todrop = 1 if regexm(company, "STATE")
replace todrop = 1 if regexm(company, "NATURAL")
replace todrop = 1 if regexm(company, "ASSOCIATES")
replace todrop = 1 if regexm(company, "AGING")
replace todrop = 1 if regexm(company, "OPTIC")
replace todrop = 1 if regexm(company, "MEDGROUP")
replace todrop = 1 if regexm(company, "INTEGRATED")
replace todrop = 1 if regexm(company, "FITNESS")
replace todrop = 1 if regexm(company, "UROLO")
replace todrop = 1 if regexm(company, "INSPIRATIONS")
replace todrop = 1 if regexm(company, "PINERIDGE")
replace todrop = 1 if regexm(company, "ESTATES")
replace todrop = 1 if regexm(company, "ASSOC")
replace todrop = 1 if regexm(company, "SKIN")
replace todrop = 1 if regexm(company, "WELL")
replace todrop = 1 if regexm(company, "ATLANTIC")
replace todrop = 1 if regexm(company, "SHORELINE")
replace todrop = 1 if regexm(company, "STATION")
replace todrop = 1 if regexm(company, "ALATA")
replace todrop = 1 if regexm(company, "MANAGEMENT")
replace todrop = 1 if regexm(company, "PMPEDIATRIC")
replace todrop = 1 if regexm(company, "PURPOSE")
replace todrop = 1 if regexm(company, "OZONE")
replace todrop = 1 if regexm(company, "DYNAMIC")
replace todrop = 1 if regexm(company, "ONCOLOGY")
replace todrop = 1 if regexm(company, "SCOTTWHITE")
replace todrop = 1 if regexm(company, "CLINIC")
replace todrop = 1 if regexm(company, "INTERMOUNTAIN")
replace todrop = 1 if regexm(company, "HEALTHGROUP")
replace todrop = 1 if regexm(company, "MDCENT")
replace todrop = 1 if regexm(company, "SEXUAL")
replace todrop = 1 if regexm(company, "ORTHO")
replace todrop = 1 if regexm(company, "THORA")
replace todrop = 1 if regexm(company, "FOUNDATION") 
replace todrop = 1 if regexm(company, "HEALTHCTR")
replace todrop = 1 if regexm(company, "THERAPY")
replace todrop = 1 if regexm(company, "BEHAVIOR")
replace todrop = 1 if regexm(company, "INTERNAL")
replace todrop = 1 if regexm(company, "DIGESTIVE")
replace todrop = 1 if regexm(company, "ORTHO")
replace todrop = 1 if regexm(company, "THORA")
replace todrop = 1 if regexm(company, "FOUNDATION") 
replace todrop = 1 if regexm(company, "HEALTHCTR")
replace todrop = 1 if regexm(company, "THERAPY")
replace todrop = 1 if regexm(company, "BEHAVIOR")
replace todrop = 1 if regexm(company, "INTERNAL")
replace todrop = 1 if regexm(company, "DIGESTIVE")
replace todrop = 1 if regexm(company, "BILLING")
replace todrop = 1 if regexm(company, "HEART")
replace todrop = 1 if regexm(company, "LUNG")
replace todrop = 1 if regexm(company, "HEARING")
replace todrop = 1 if regexm(company, "LASIK")
replace todrop = 1 if regexm(company, "COUNSELING")
replace todrop = 1 if regexm(company, "POOL")
replace todrop = 1 if regexm(company, "DENTAL")
replace todrop = 1 if regexm(company, "CARO")
replace todrop = 1 if regexm(company, "RHB")
replace todrop = 1 if regexm(company, "UNIVERSITY")
replace todrop = 1 if regexm(company, "MGMT")
replace todrop = 1 if regexm(company, "LIVING")
replace todrop = 1 if regexm(company, "CENTER") & !regexm(company, "HEALTH")
replace todrop = 1 if regexm(company, "WOMEN")
replace todrop = 1 if regexm(company, "PSY")
replace todrop = 1 if regexm(company, "MENTAL")
replace todrop = 1 if regexm(company, "TOUCH")
replace todrop = 1 if regexm(company, "FERNANDO")
replace todrop = 1 if regexm(company, "SWLA")
replace todrop = 1 if regexm(company, "CHILDREN")
replace todrop = 1 if regexm(company, "NURSE")
replace todrop = 1 if regexm(company, "ENDOCRI")
replace todrop = 1 if regexm(company, "PROMOTION")
replace todrop = 1 if regexm(company, "TUTOR")
replace todrop = 1 if regexm(company, "INJURY")
replace todrop = 1 if regexm(company, "DEPARTMENT")
replace todrop = 1 if regexm(company, "COMMUNITY")
replace todrop = 1 if regexm(company, "OPTOME")
replace todrop = 1 if regexm(company, "FLEXOLOGY")
replace todrop = 1 if regexm(company, "CNSLNG")
replace todrop = 1 if regexm(company, "FAMILY")
replace todrop = 1 if regexm(company, "RESOURCES")
replace todrop = 1 if regexm(company, "DERMA")
replace todrop = 1 if regexm(company, "PERIOD")
replace todrop = 1 if regexm(company, "ASTHMA")
replace todrop = 1 if regexm(company, "FILING")
replace todrop = 1 if regexm(company, "THERAPY")
replace todrop = 1 if regexm(company, "THERAPEUTIC")
replace todrop = 1 if regexm(company, "ADVANCE")
replace todrop = 1 if regexm(company, "SYSTEM")
replace todrop = 1 if regexm(company, "PRIMARY") & !regexm(company, "URGENT")
replace todrop = 1 if regexm(company, "OSTEO")
replace todrop = 1 if regexm(company, "BREAST")
replace todrop = 1 if regexm(company, "DENTIS")
replace todrop = 1 if regexm(company, "ACCUP")
replace todrop = 1 if regexm(company, "NETWORK")
replace todrop = 1 if regexm(company, "OCCUPATION")
replace todrop = 1 if regexm(company, "INTEGRATIVE")
replace todrop = 1 if regexm(company, "VIK")
replace todrop = 1 if regexm(company, "CMNTY")
replace todrop = 1 if regexm(company, "ILLUMI")
replace todrop = 1 if regexm(company, "CENTERFORHEALTH")
replace todrop = 1 if regexm(company, "KIDDS")
replace todrop = 1 if regexm(company, "ELDERLY")
replace todrop = 1 if regexm(company, "VISITING")
replace todrop = 1 if regexm(company, "INC")
replace todrop = 1 if regexm(company, "NUTRITION")
replace todrop = 1 if regexm(company, "SYST")
replace todrop = 1 if regexm(company, "VISION")
replace todrop = 1 if regexm(company, "LABS")
replace todrop = 1 if regexm(company, "CHILD")
replace todrop = 1 if regexm(company, "STMARY")
replace todrop = 1 if regexm(company, "RETINA")
replace todrop = 1 if regexm(company, "INTEGRATIVE")
replace todrop = 1 if regexm(company, "BALANCED")
replace todrop = 1 if regexm(company, "NASHUA")
replace todrop = 1 if regexm(company, "DEPT")
replace todrop = 1 if regexm(company, "DEPARTMENT")
replace todrop = 1 if regexm(company, "LUBITZ")
replace todrop = 1 if regexm(company, "SPINAL")
replace todrop = 1 if regexm(company, "ARTTECH")
replace todrop = 1 if regexm(company, "PRIVATE")
replace todrop = 1 if regexm(company, "LEXIUS")
replace todrop = 1 if regexm(company, "HOSPICE")
replace todrop = 1 if regexm(company, "CORP")
replace todrop = 1 if regexm(company, "LIFETIME")
replace todrop = 1 if regexm(company, "OPTIMAL")
replace todrop = 1 if regexm(company, "SPORT")
replace todrop = 1 if regexm(company, "RADIOLOGY")
replace todrop = 1 if regexm(company, "PRINT")
replace todrop = 1 if regexm(company, "ELDER")
replace todrop = 1 if regexm(company, "SPECS")
replace todrop = 1 if regexm(company, "ALANDAYAN")
replace todrop = 1 if regexm(company, "SENIOR")
replace todrop = 1 if regexm(company, "DOCTORSOFFICE")
replace todrop = 1 if regexm(company, "HOLISTIC")
replace todrop = 1 if regexm(company, "SUPPORT")
replace todrop = 1 if regexm(company, "PELVIC")
replace todrop = 1 if regexm(company, "TRADITION")
replace todrop = 1 if regexm(company, "PHYSICAL")
replace todrop = 1 if regexm(company, "CARDIO")
replace todrop = 1 if regexm(company, "SVC")
replace todrop = 1 if regexm(company, "RURAL")
replace todrop = 1 if regexm(company, "DOCTOROFFICE")
replace todrop = 1 if regexm(company, "REPRODUC")
replace todrop = 1 if regexm(company, "THROAT")
replace todrop = 1 if regexm(company, "LEGACY")
replace todrop = 1 if regexm(company, "DOCTORSOFFICE")
replace todrop = 1 if regexm(company, "LABOR")
replace todrop = 1 if regexm(company, "HERBS")
replace todrop = 1 if regexm(company, "LEVIN")
replace todrop = 1 if regexm(company, "JENNIFER")
replace todrop = 1 if regexm(company, "EXCELLENCE")
replace todrop = 1 if regexm(company, "CONNECTED")
replace todrop = 1 if regexm(company, "HAZAR")
replace todrop = 1 if regexm(company, "CHRISTIAN")
replace todrop = 1 if regexm(company, "UNITED")
replace todrop = 1 if regexm(company, "DDSO")
replace todrop = 1 if regexm(company, "STELLA")
replace todrop = 1 if regexm(company, "HEALING")
replace todrop = 1 if regexm(company, "NTWRK")
replace todrop = 1 if regexm(company, "SLEEP")
replace todrop = 1 if regexm(company, "EXCELLANCE")
replace todrop = 1 if regexm(company, "OFFICE")
replace todrop = 1 if regexm(company, "FRED")
replace todrop = 1 if regexm(company, "AUDIOLOGY")
replace todrop = 1 if regexm(company, "LOVE")
replace todrop = 1 if regexm(company, "INDEPEN")
replace todrop = 1 if regexm(company, "BOROSS")
replace todrop = 1 if regexm(company, "CITRUS")
replace todrop = 1 if regexm(company, "COMM")
replace todrop = 1 if regexm(company, "GENHEAL")
replace todrop = 1 if regexm(company, "LOSANGELES")
replace todrop = 1 if regexm(company, "TRAVELING")
replace todrop = 1 if regexm(company, "DIVERSI")
replace todrop = 1 if regexm(company, "SPINE")
replace todrop = 1 if regexm(company, "POPLAR")
replace todrop = 1 if regexm(company, "NUTRA")
replace todrop = 1 if regexm(company, "ILUMI")
replace todrop = 1 if regexm(company, "PAIN")
replace todrop = 1 if regexm(company, "PROGRAM")
replace todrop = 1 if regexm(company, "HONOR")
replace todrop = 1 if regexm(company, "CHINLE")
replace todrop = 1 if regexm(company, "ASSESS")
replace todrop = 1 if regexm(company, "RESIDEN")
replace todrop = 1 if regexm(company, "DAVITA")
replace todrop = 1 if regexm(company, "AFFORD")
replace todrop = 1 if regexm(company, "CHICTR")
replace todrop = 1 if regexm(company, "WOMAN")
replace todrop = 1 if regexm(company, "PHYSHEALTH")
replace todrop = 1 if regexm(company, "ORGANICS")
replace todrop = 1 if regexm(company, "KAISER")
replace todrop = 1 if regexm(company, "INDIANHEALTH")
replace todrop = 1 if regexm(company, "ADVENTIST")
replace todrop = 1 if regexm(company, "PROVIDENCE")
replace todrop = 1 if regexm(company, "WENDY")
replace todrop = 1 if regexm(company, "MERCY")
replace todrop = 1 if regexm(company, "GROUP")
replace todrop = 1 if regexm(company, "ROMINE")
replace todrop = 1 if regexm(company, "MEMORIAL")
replace todrop = 1 if regexm(company, "MEML")
replace todrop = 1 if regexm(company, "PUBLIC")
replace todrop = 1 if regexm(company, "PLAN")
replace todrop = 1 if regexm(company, "CNSRTM")
replace todrop = 1 if regexm(company, "GRP")
replace todrop = 1 if regexm(company, "CTR") & !regexm(company, "URGENT")
replace todrop = 1 if regexm(company, "CENTER") & !regexm(company, "URGENT")
replace todrop = 1 if regexm(company, "HOSP")
replace todrop = 1 if regexm(company, "ACUP")
replace todrop = 1 if regexm(company, "TEAM")
replace todrop = 1 if regexm(company, "CLOVIS")
replace todrop = 1 if regexm(company, "LLC")
replace todrop = 1 if regexm(company, "SPRT")
replace todrop = 1 if regexm(company, "SPECIAL")
replace todrop = 1 if regexm(company, "SPCLTY")
replace todrop = 1 if regexm(company, "ADVANTAGE")
replace todrop = 1 if regexm(company, "CHLDRNS")
replace todrop = 1 if regexm(company, "FOOT")
replace todrop = 1 if regexm(company, "KACZA")
replace todrop = 1 if regexm(company, "ASSN")
replace todrop = 1 if regexm(company, "ASSO")
replace todrop = 1 if regexm(company, "BECOME")
replace todrop = 1 if regexm(company, "MASCAREN")
replace todrop = 1 if regexm(company, "SMILE")
replace todrop = 1 if regexm(company, "COASTAL")
replace todrop = 1 if regexm(company, "PREVENTIONS")
replace todrop = 1 if regexm(company, "SOLUTION")
replace todrop = 1 if regexm(company, "STRATEGIC")
replace todrop = 1 if regexm(company, "INTL")
replace todrop = 1 if regexm(company, "LAB")
replace todrop = 1 if regexm(company, "COACH")
replace todrop = 1 if regexm(company, "UNITY")
replace todrop = 1 if regexm(company, "MENSHEALTH")
replace todrop = 1 if regexm(company, "AURORA")
replace todrop = 1 if regexm(company, "HSHS")
replace todrop = 1 if regexm(company, "DRMARY")
replace todrop = 1 if regexm(company, "CRYSTAL")
replace todrop = 1 if regexm(company, "ALEXIAN")
replace todrop = 1 if regexm(company, "MACNEAL")
replace todrop = 1 if regexm(company, "PANIC")
replace todrop = 1 if regexm(company, "CRITICAL")
replace todrop = 1 if regexm(company, "HOUSE")
replace todrop = 1 if regexm(company, "BAPTIST") & !regexm(company, "URGENT")
replace todrop = 1 if regexm(company, "KENTUCKY")
replace todrop = 1 if regexm(company, "APPALA")
replace todrop = 1 if regexm(company, "SUMMIT")
replace todrop = 1 if regexm(company, "FUTURE")
replace todrop = 1 if regexm(company, "CHICOPEE")
replace todrop = 1 if regexm(company, "MERRIMACK")
replace todrop = 1 if regexm(company, "PRODUCT")
replace todrop = 1 if regexm(company, "HOPKINS")
replace todrop = 1 if regexm(company, "HENRY")
replace todrop = 1 if regexm(company, "HUMANA")
replace todrop = 1 if regexm(company, "PODIATRY")
replace todrop = 1 if regexm(company, "PASTORAL")
replace todrop = 1 if regexm(company, "PRESBYT")
replace todrop = 1 if regexm(company, "COOK")
replace todrop = 1 if regexm(company, "RESPI")
replace todrop = 1 if regexm(company, "KAZA")
replace todrop = 1 if regexm(company, "WEIGHT")
replace todrop = 1 if regexm(company, "YOUR")
replace todrop = 1 if regexm(company, "JONESVILLE")
replace todrop = 1 if regexm(company, "BRONSON") & !regexm(company, "URGENT")
replace todrop = 1 if regexm(company, "LILLEY")
replace todrop = 1 if regexm(company, "SOUND")
replace todrop = 1 if regexm(company, "MEDICINE")
replace todrop = 1 if regexm(company, "LADIES")
replace todrop = 1 if regexm(company, "RIDGEVIEW")
replace todrop = 1 if regexm(company, "CENTRACARE") & !regexm(company, "URGENT")
replace todrop = 1 if regexm(company, "BENCHMARK")
replace todrop = 1 if regexm(company, "CONSUMER")
replace todrop = 1 if regexm(company, "MOSAIC")
replace todrop = 1 if regexm(company, "LUEZA")
replace todrop = 1 if regexm(company, "PROACTIVE")
replace todrop = 1 if regexm(company, "RICCO")
replace todrop = 1 if regexm(company, "BREESE")
replace todrop = 1 if regexm(company, "NOVANT")
replace todrop = 1 if regexm(company, "IMAGE")
replace todrop = 1 if regexm(company, "VALENCIA")
replace todrop = 1 if regexm(company, "SPTLST")
replace todrop = 1 if regexm(company, "AMBULATORY")
replace todrop = 1 if regexm(company, "AHOLLY")
replace todrop = 1 if regexm(company, "MTSINAI")
replace todrop = 1 if regexm(company, "RUNNING")
replace todrop = 1 if regexm(company, "SAMUEL")
replace todrop = 1 if regexm(company, "DETOX")
replace todrop = 1 if regexm(company, "COALITION")
replace todrop = 1 if regexm(company, "NOSE")
replace todrop = 1 if regexm(company, "UNIQUE")
replace todrop = 1 if regexm(company, "MACHIAS")
replace todrop = 1 if regexm(company, "BLANCHARD")
replace todrop = 1 if regexm(company, "CATHOLIC")
replace todrop = 1 if regexm(company, "FRONTLINE")
replace todrop = 1 if regexm(company, "CHINESE")
replace todrop = 1 if regexm(company, "BLOSSOM")
replace todrop = 1 if regexm(company, "SOURCE")
replace todrop = 1 if regexm(company, "GLOBE")
replace todrop = 1 if regexm(company, "INVSTMNT")
replace todrop = 1 if regexm(company, "LANCASTER")
replace todrop = 1 if regexm(company, "DOCTORELIO")
replace todrop = 1 if regexm(company, "GASTRO")
replace todrop = 1 if regexm(company, "BARHII")
replace todrop = 1 if regexm(company, "BLUE")
replace todrop = 1 if regexm(company, "BLACK")
replace todrop = 1 if regexm(company, "MUSCHE")
replace todrop = 1 if regexm(company, "MARIAS")
replace todrop = 1 if regexm(company, "CHING")
replace todrop = 1 if regexm(company, "CONCHO")
replace todrop = 1 if regexm(company, "APLUS")
replace todrop = 1 if regexm(company, "FORWMN")
replace todrop = 1 if regexm(company, "BENJAMIN")
replace todrop = 1 if regexm(company, "JOHNDOCK")
replace todrop = 1 if regexm(company, "INFECT")
replace todrop = 1 if regexm(company, "DOCTEURE")
replace todrop = 1 if regexm(company, "CASCADE")
replace todrop = 1 if regexm(company, "FACILITY")
replace todrop = 1 if regexm(company, "AVAMERE")
replace todrop = 1 if regexm(company, "KLICIT")
replace todrop = 1 if regexm(company, "KITTITAS")
replace todrop = 1 if regexm(company, "CITIES")
replace todrop = 1 if regexm(company, "PHARMA")
replace todrop = 1 if regexm(company, "AGNES")
replace todrop = 1 if regexm(company, "SPECIAL")
replace todrop = 1 if regexm(company, "UWHEALTH")
replace todrop = 1 if regexm(company, "VALLEYHEALTH") & !regexm(company, "URGENT")
replace todrop = 1 if regexm(company, "DOCTORSLAWACZECK")
replace todrop = 1 if regexm(company, "JRMCLIFELINE")
replace todrop = 1 if regexm(company, "SEVENTY")
replace todrop = 1 if regexm(company, "POMONAADULTDAYHEALTHCARE")
replace todrop = 1 if regexm(company, "TOTALHEALTHMEDOFTHEDESERT")
replace todrop = 1 if regexm(company, "CONGREGATE")
replace todrop = 1 if regexm(company, "HEALTHBEGINNING")
replace todrop = 1 if regexm(company, "CONTRACOSTACAREGIVERS")
replace todrop = 1 if regexm(company, "WORLDHEALTHCAREPARTNERS")
replace todrop = 1 if regexm(company, "EASTBAYCOMPREHENSIVEHEALTH")
replace todrop = 1 if regexm(company, "WESTCOVINAKIDSDOC")
replace todrop = 1 if regexm(company, "DOCTORKIMSSAT")
replace todrop = 1 if regexm(company, "VALLEYHILLSHMHEALTHCARE")
replace todrop = 1 if regexm(company, "ABILINGUALDOCTOR")
replace todrop = 1 if regexm(company, "COOPERATIVE")
replace todrop = 1 if regexm(company, "DINGHYDOCTOR")
replace todrop = 1 if regexm(company, "KERNLIFELINEPROJECT")
replace todrop = 1 if regexm(company, "ACPUNCTURE")
replace todrop = 1 if regexm(company, "UCHEALTHKARENDEL")
replace todrop = 1 if regexm(company, "BIONETICHEAL")
replace todrop = 1 if regexm(company, "EXCELLENTPERSONAL")
replace todrop = 1 if regexm(company, "UCHEALTHGLENN")
replace todrop = 1 if regexm(company, "PHILIPSLIFELINE")
replace todrop = 1 if regexm(company, "ANESTHESIA")
replace todrop = 1 if regexm(company, "RUDDOCK")
replace todrop = 1 if regexm(company, "CHIPOLAHEALTHSTARTMOMCARE")
replace todrop = 1 if regexm(company, "GOMEXCARE")
replace todrop = 1 if regexm(company, "DRJOHNCNOWAK")
replace todrop = 1 if regexm(company, "DOCTORREFERRALLINE")
replace todrop = 1 if regexm(company, "LIFELINESTEMCELL")
replace todrop = 1 if regexm(company, "VIACHRISTI")
replace todrop = 1 if regexm(company, "HUTCHINSONAREASTUDENT")
replace todrop = 1 if regexm(company, "DRCURTISDSNOWDEN")
replace todrop = 1 if regexm(company, "CUMBERLANDVALLEYDISTHEALTH")
replace todrop = 1 if regexm(company, "GIARDINAGLUBODOCTOR")
replace todrop = 1 if regexm(company, "HUNTVALLEYMARTPHARM")
replace todrop = 1 if regexm(company, "ENDOCORINE")
replace todrop = 1 if regexm(company, "ANDREANOWAK")
replace todrop = 1 if regexm(company, "DESMOINESVALLEYPUBHEALTH")
replace todrop = 1 if regexm(company, "ACHIEVEHEALTHBACKTOBALANCE")
replace todrop = 1 if regexm(company, "VEINDOCTOR")
replace todrop = 1 if regexm(company, "DRJOHNSNOW")
replace todrop = 1 if regexm(company, "DOCTORSVITALSCREENING")
replace todrop = 1 if regexm(company, "LODGEATWAKEFOREST")
replace todrop = 1 if regexm(company, "WAKEFORESTUNIVSTUDENTHLTH")
replace todrop = 1 if regexm(company, "XGENESYS")
replace todrop = 1 if regexm(company, "EXCELOPERATING")
replace todrop = 1 if regexm(company, "SPINDOCTOR")
replace todrop = 1 if regexm(company, "CENTERFOREXCELLENCEINPERI")
replace todrop = 1 if regexm(company, "ROLANDOCIBIS")
replace todrop = 1 if regexm(company, "DOCTORSINGHVI")
replace todrop = 1 if regexm(company, "DRSCOTTSHACK")
replace todrop = 1 if regexm(company, "MARCGOLDSTEIN")
replace todrop = 1 if regexm(company, "EXCELERATEHEALTHCARE")
replace todrop = 1 if regexm(company, "GENERALPRACTITIONERSLIFECARE")
replace todrop = 1 if regexm(company, "ONETOONEFEMALECARE")
replace todrop = 1 if regexm(company, "DISTRIBUTION")
replace todrop = 1 if regexm(company, "ANMEDHEALTHCANNONLIBERTY")
replace todrop = 1 if regexm(company, "BUSINESSAFTERHORS")
replace todrop = 1 if regexm(company, "DOCTORBOWLING")

replace todrop = 0 if regexm(company, "MEDEXPR")
replace todrop = 0 if regexm(company, "FASTPACE") 
replace todrop = 0 if regexm(company, "HEALTHCAREPARTNERS") 
replace todrop = 1 if regexm(company, "HEALTHCAREPARTNERS") & (regexm(company, "LLC") | regexm(company, "ASPIRE") | regexm(company, "FOCUSED") | regexm(company, "FAMILY") | regexm(company, "INC") | regexm(company, "SENIOR") | regexm(company, "SOUTH") | regexm(company, "WOMAN") | regexm(company, "COPLEY") | regexm(company, "DAVINCI") | regexm(company, "STRATEGIC") | regexm(company, "IGUANA") | regexm(company, "HOME") | regexm(company, "RCC") | regexm(company, "WELLNESS") | regexm(company, "CCO") | regexm(company, "ANESTHE") | regexm(company, "BEHAVIORAL") | regexm(company, "INFINITE") | regexm(company, "MANAGED") | regexm(company, "ELITE") | regexm(company, "USA") | regexm(company, "VALLEY") | regexm(company, "DW") | regexm(company, "SPECIALTY") | regexm(company, "WOMEN") | regexm(company, "LTD") |regexm(company, "MAGIC") | regexm(company, "DAVITA") | regexm(company, "ECI") | regexm(company, "DF") | regexm(company, "CONSULTING") | regexm(company, "COMMUNITY") | regexm(company, "CATHOLIC") | regexm(company, "GLOBAL") | regexm(company, "IPA") | regexm(company, "MADISON") | regexm(company, "HUNTER") | regexm(company, "2020") | regexm(company, "MAXUS") | regexm(company, "SIGNET") | regexm(company, "PROVIDENCE") | regexm(company, "AFFILIATES") | regexm(company, "ASSOC") | regexm(company, "WORLD") | regexm(company, "LIGHT") | regexm(company, "ZN") | regexm(company, "ODYSSEY") | regexm(company, "JHD") | regexm(company, "FOUNTAIN") | regexm(company, "DESERT") | regexm(company, "WINSTON") | regexm(company, "DIRECT") | regexm(company, "DOMINION") | regexm(company, "MSL") | regexm(company, "EVERGREEN") | regexm(company, "MEDICAL") | regexm(company, "CRDL"))
replace todrop = 0 if regexm(company, "PARTNERS") & regexm(company, "URGENT") 
replace todrop = 0 if regexm(company, "PARTNERS") & regexm(company, "WENT") 
replace todrop = 0 if regexm(company, "NOVANTHEALTH") 
replace todrop = 0 if regexm(company, "APPLE") & regexm(company, "CARE")
replace todrop = 0 if regexm(company, "APPLE") & regexm(company, "URGENT")
replace todrop = 0 if regexm(company, "RENOWN") & regexm(company, "HEALTH")
replace todrop = 0 if regexm(company, "MEDFAST") 
replace todrop = 0 if regexm(company, "MEDCARE") & regexm(company, "URGENT")
replace todrop = 0 if regexm(company, "MEDCARE") & regexm(company, "XPRESS")
replace todrop = 0 if regexm(company, "MEDCARE") & regexm(company, "DOCTORS")
replace todrop = 0 if regexm(company, "K+CLINIC") 
replace todrop = 0 if regexm(company, "INSPIRA") & regexm(company, "HEALTH")
replace todrop = 0 if regexm(company, "INSPIRA") & regexm(company, "NETWORK")
replace todrop = 1 if regexm(company, "INSPIRATIONS")
replace todrop = 0 if regexm(company, "RADY") 
replace todrop = 0 if regexm(company, "FASTTRACK") 
replace todrop = 0 if regexm(company, "EMERGICARE") 
replace todrop = 0 if regexm(company, "AVON") & regexm(company, "CARE")
replace todrop = 0 if regexm(company, "FIRSTMED") & regexm(company, "IMME")
replace todrop = 0 if regexm(company, "FIRSTMED") & regexm(company, "URGENT")
replace todrop = 0 if regexm(company, "FIRSTCARE") & regexm(company, "CLINIC")
replace todrop = 0 if regexm(company, "FIRSTCARE") & regexm(company, "URGENT")
replace todrop = 0 if regexm(company, "FIRSTHELP") & regexm(company, "URGENT")
replace todrop = 0 if regexm(company, "CARESTAT") 
replace todrop = 0 if regexm(company, "PRIMECARE") 
replace todrop = 1 if regexm(company, "PRIMECARE") & regexm(company, "MEDICAL")
replace todrop = 1 if regexm(company, "PRIMECARE") & (regexm(company, "FAMILY") | regexm(company, "SUPPLIES") | regexm(company, "HOME") | regexm(company, "PHYSI") | regexm(company, "NETWORK") | regexm(company, "PEDIATRI") | regexm(company, "HEALTHCTR") | regexm(company, "REHAB") | regexm(company, "DENTAL") | regexm(company, "NEURO") | regexm(company, "SUPPLIES") | regexm(company, "INTERN") | regexm(company, "PSYCH") | regexm(company, "MEDCTR") | regexm(company, "MEDGRP") | regexm(company, "ASSOC") | regexm(company, "THERAPY") | regexm(company, "MEDICINE") | regexm(company, "CHIROP") | regexm(company, "SLEEP") | regexm(company, "COMMUNITY") | regexm(company, "ATTENTIVE") | regexm(company, "BILLING") | regexm(company, "WORK") | regexm(company, "PODIA") | regexm(company, "GIVER") | regexm(company, "AGENCY") | regexm(company, "OCCUPA") | regexm(company, "STALEX") | regexm(company, "WELLNESS") | regexm(company, "PHYSI") | regexm(company, "ORTHO") | regexm(company, "IMAGING") | regexm(company, "INFECT"))
replace todrop = 0 if regexm(company, "CAREEXPRE") 
replace todrop = 0 if regexm(company, "STATMED") 
replace todrop = 0 if regexm(company, "PARKMED") 
replace todrop = 0 if regexm(company, "QUICKCARE") 
replace todrop = 0 if regexm(company, "ONPOINT") 
replace todrop = 0 if regexm(company, "METROMED") 
replace todrop = 0 if regexm(company, "ICAREONE") 
replace todrop = 0 if regexm(company, "FIRSTSTOP") 
replace todrop = 0 if regexm(company, "ALLCARE") 
replace todrop = 0 if regexm(company, "CAREFIRST") 
replace todrop = 0 if regexm(company, "EXCELL") 
replace todrop = 1 if regexm(company, "EXCELLENCE")
replace todrop = 1 if regexm(company, "EXCELLENT")
replace todrop = 0 if regexm(company, "PREMIERER") 
replace todrop = 0 if regexm(company, "GETMED") 
replace todrop = 0 if regexm(company, "DMHEXPR") 
replace todrop = 0 if regexm(company, "QUICKMED") 
replace todrop = 0 if regexm(company, "MEADVILLE") 
replace todrop = 0 if regexm(company, "CARECLINIC") & regexm(company, "LAREDO")
replace todrop = 0 if regexm(company, "CARE4") 
replace todrop = 0 if regexm(company, "URGENTMED") 
replace todrop = 0 if regexm(company, "BILLINGSCLINIC") 
replace todrop = 0 if regexm(company, "EXPRESSCARE") 
replace todrop = 0 if regexm(company, "APEX") & regexm(company, "CARE")
replace todrop = 0 if regexm(company, "ACCELERATED") & regexm(company, "CARE")
replace todrop = 0 if regexm(company, "LOGANREGIONALMEDICAL") | (regexm(company, "NOVANTHEALTHMEDICALGROUP") & state=="NC")| (regexm(company, "2FAMILYWALKINCLINIC") & state=="TN") | (regexm(company, "SUMMITFAMILY") & state=="MS") | (regexm(company, "PROMPTMED") & state=="IN")

replace todrop = 0 if regexm(company, "PROMEDCOOPERCLINIC")
replace todrop = 0 if regexm(company, "RIVERVALLERYHEALTHR")
replace todrop = 0 if regexm(company, "PIONEERMEDICALCLINIC")
replace todrop = 0 if regexm(company, "PHYSICIANSIMMEDIATECARECTR")
replace todrop = 0 if regexm(company, "SPRINGFIELDCLINICPROMPTCARE")
replace todrop = 0 if regexm(company, "PROMPTCAREJACKSONVILLE")
replace todrop = 0 if regexm(company, "PARKWAYCENTER")
replace todrop = 0 if regexm(company, "DOCTORSPARKFAMILYMEDICINE")
replace todrop = 0 if regexm(company, "INJURYCARECLINICSIOUXLAND")
replace todrop = 0 if regexm(company, "YOURDOCTORSIN")
replace todrop = 0 if regexm(company, "EXPRESSCAREOFSALISBURY")
replace todrop = 0 if regexm(company, "PROMEDCOOPERCLINIC")
replace todrop = 0 if regexm(company, "PATIENTFIRSTHOMECAREINC") & state=="MI"
replace todrop = 0 if regexm(company, "MEDEXPRESS") & state=="MI"
replace todrop = 0 if regexm(company, "PATIENTFIRSTHMINFUSIONSVC")
replace todrop = 0 if regexm(company, "AFTERHOURSDENTALCLINICINC") & state=="MS"
replace todrop = 0 if regexm(company, "IMMEDIATECAREFAMILYCLINIC") & state=="MS"
replace todrop = 0 if regexm(company, "STATSAREPLUSPLLC") & state=="MS"
replace todrop = 0 if regexm(company, "RIVERSIDEMEDICAL") & state=="NY"
replace todrop = 0 if regexm(company, "NOVANTURGENT") & state=="NC"
replace todrop = 0 if regexm(company, "ONEONONECAREINC") & state=="NC"
replace todrop = 0 if regexm(company, "CONVENIENTCARECLINIC") & state=="ND"
replace todrop = 0 if regexm(company, "IMMEDIATECARECLINIC") & state=="OR"
replace todrop = 0 if regexm(company, "AVERADAKOTAMEDICALCLINIC") & state=="SD"
replace todrop = 0 if regexm(company, "TRINITYCLINIC") & state=="TX"
replace todrop = 0 if regexm(company, "DONNADAYNIGHTCLINIC") & state=="TX"
replace todrop = 0 if regexm(company, "COMPLETECARECLINIC") & state=="TX"
replace todrop = 0 if regexm(company, "NEIGHBORSEMERGENEYCTR") & state=="TX"
replace todrop = 0 if regexm(company, "PIONEERMEDICALSVC") & state=="WV"
replace todrop = 0 if regexm(company, "RIVERSIDEMEDICALCLINIC") & state=="WI"
replace todrop = 0 if regexm(company, "HEALTHCAREPARTNERSINC") & state=="NY"
replace todrop = 0 if regexm(company, "WAKEFORESTBAPMEDCTRCMNTY") & state=="NC"
replace todrop = 0 if regexm(company, "FAMILYFIRSTMEDICALASSOC") & state=="GA"
replace todrop = 0 if regexm(company, "CADILLACAFTERHOURSCLINIC") & state=="MI"
replace todrop = 0 if regexm(company, "JBAFFORDABLEHEALTHCARE") & state=="WI"
replace todrop = 0 if regexm(company, "BAYAREHEALTHCTR") & state=="MN"
replace todrop = 0 if regexm(company, "ILLEXPRESSEYECARE") & state=="IL"
replace todrop = 0 if regexm(company, "ACCUCARECLINICOFNORTHTEXAS") & state=="TX"
replace todrop = 0 if regexm(company, "DOCTORSOFFICE") & state=="TX"
drop if state=="HI"
drop if state=="AK"
drop if todrop
drop todrop
drop is_*
tab year
save "UrgentCareMain_R3_base.dta", replace

use "UrgentCareMain_R3_base.dta", clear
append using "AdditionalUCCs_R3f.dta", force
keep if year>2004 & year<2018
duplicates drop abi year, force
duplicates drop Longitude Latitude year, force
gen todrop = 0
replace todrop = 1 if regexm(company, "MERCY")
replace todrop = 1 if regexm(company, "CHIROPRACTIC")
replace todrop = 1 if regexm(company, "BAPTIST") & !regexm(company, "URGENT")
replace todrop = 1 if regexm(company, "INTERNAL MEDICINE")
replace todrop = 1 if regexm(company, "MAYO")
replace todrop = 1 if regexm(company, "PRESBYTERIAN")
replace todrop = 1 if regexm(company, "MEMORIAL")
replace todrop = 1 if regexm(company, "ST LUKE")
replace todrop = 1 if regexm(company, "ELIZABETH")
replace todrop = 1 if regexm(company, "ST MARY")
replace todrop = 1 if regexm(company, "SAMARITAN")
replace todrop = 1 if regexm(company, "DEACONESS")
replace todrop = 1 if regexm(company, "ADVENTIST")
replace todrop = 1 if regexm(company, "ST ANDREWS")
replace todrop = 1 if regexm(company, "ST FRANCIS")
replace todrop = 1 if regexm(company, "JOSEPH")
replace todrop = 1 if regexm(company, "ORANGE REGIONAL")
replace todrop = 1 if regexm(company, "METHODIST")
replace todrop = 1 if regexm(company, "ST JOHN")
replace todrop = 1 if regexm(company, "ST CATHERINE")
replace todrop = 1 if regexm(company, "MENNONITE")
replace todrop = 1 if regexm(company, "LAFAYETTE")
replace todrop = 1 if regexm(company, "CATHOLIC")
replace todrop = 1 if regexm(company, "PROVIDENCE VA")
replace todrop = 1 if regexm(company, "ROGER WILLIAMS")
replace todrop = 1 if regexm(company, "EXETER")
replace todrop = 1 if regexm(company, "HOSPITAL")
replace todrop = 1 if regexm(company, "HOSP")
replace todrop = 1 if regexm(company, "HOPKINS")
replace todrop = 1 if regexm(company, "MASSACHUSETTS GENERAL")
replace todrop = 1 if regexm(company, "MASS GENEARL")
replace todrop = 1 if regexm(company, "UCSF")
replace todrop = 1 if regexm(company, "RONAL REAGAN")
replace todrop = 1 if regexm(company, "CLEVELAND CLINIC")
replace todrop = 1 if regexm(company, "NORTHWESTERN MEMORIAL")
replace todrop = 1 if regexm(company, "MT SINAI")
replace todrop = 1 if regexm(company, "MOUNT SINAI")
replace todrop = 1 if regexm(company, "CEDARS")
replace todrop = 1 if regexm(company, "BRIGHAM")
replace todrop = 1 if regexm(company, "JACKSON MEMORIAL")
replace todrop = 1 if regexm(company, "ADVENTHEALTH")
replace todrop = 1 if regexm(company, "ADVENT HEALTH")
replace todrop = 1 if regexm(company, "TRINITY HEALTH")
replace todrop = 1 if regexm(company, "THOMAS")
replace todrop = 1 if regexm(company, "BAYLOR")
replace todrop = 1 if regexm(company, "BAYLOR") & regexm(company, "SCOTT") & regexm(company, "WHITE")
replace todrop = 1 if regexm(company, "YALE")
replace todrop = 1 if regexm(company, "VANDERBILT")
replace todrop = 1 if regexm(company, "TENET HEALTHCARE")
replace todrop = 1 if regexm(company, "KAISER")
replace todrop = 1 if regexm(company, "BROOKS MEMORIAL")
replace todrop = 1 if regexm(company, "ROCHESTER GENERAL")
replace todrop = 1 if regexm(company, "ALLEGHENY GENERAL")
replace todrop = 1 if regexm(company, "ST CLAIR")
replace todrop = 1 if regexm(company, "UPMC BEDFORD")
replace todrop = 1 if regexm(company, "SOMERSET")
replace todrop = 1 if regexm(company, "EXCELA")
replace todrop = 1 if regexm(company, "PENN HIGHLANDS")
replace todrop = 1 if regexm(company, "SYSTEM")
replace todrop = 1 if regexm(company, "NETWORK")
replace todrop = 1 if regexm(company, "BANNER")
replace todrop = 1 if regexm(company, "UPMC")
replace todrop = 1 if regexm(company, "UNITY") & regexm(company, "POINT")
replace todrop = 1 if regexm(company, "ARDENT")
replace todrop = 1 if regexm(company, "SSM HEALTH")
replace todrop = 1 if regexm(company, "SUTTER HEALTH")
replace todrop = 1 if regexm(company, "QUORUM")
replace todrop = 1 if regexm(company, "ASCENSION")
replace todrop = 1 if regexm(company, "BEAUMONT")
replace todrop = 1 if regexm(company, "ATRIUM")
replace todrop = 1 if regexm(company, "FORSYTH")
replace todrop = 1 if regexm(company, "MEDSTAR WASHINGTON")
replace todrop = 1 if regexm(company, "SIGNATURE")
replace todrop = 1 if regexm(company, "OSCAR")
replace todrop = 1 if regexm(company, "BEHAVIORAL")
replace todrop = 1 if regexm(company, "BREAST")
replace todrop = 1 if regexm(company, "RESEARCH")
replace todrop = 1 if regexm(company, "RPTHC")
replace todrop = 1 if regexm(company, "PODIATRIC")
replace todrop = 1 if regexm(company, "REINALDO")
replace todrop = 1 if regexm(company, "PRACTICE")
replace todrop = 1 if regexm(company, "OCCUPATIONAL")
drop if todrop
save "UrgentCareMain_R3.dta", replace

********************************************************************************
// AUCCs
********************************************************************************

use "RawUrgentCare_R3.dta", clear
keep if a_hospital==1
tempfile operator
save `operator'

use `operator', clear
ren abi eabi
ren Latitude eLatitude
ren Longitude eLongitude
ren og_company eog_company
tempfile base1
save `base1'

use `operator', clear
tempfile base0
save `base0'

forvalues y=2005/2017{
	use `base1', clear
	keep if year==`y'
	keep eabi eLatitude eLongitude eog_company
	tempfile yearlybase1
	save `yearlybase1'
		
	use `base0', clear
	keep if year==`y'
	keep abi Latitude Longitude og_company
	tempfile yearlybase0
	save `yearlybase0'
	if(_N!=0){
		capture quietly{
			geonear abi Latitude Longitude using `yearlybase1', n(eabi eLatitude eLongitude) long ignoreself within(0.1)
			merge m:1 eabi using `yearlybase1'
			keep if _merge==3
			drop _merge
			ren abi abi1
			ren eabi abi2
			gen delete_eabi = abi2 if km_to_eabi<=0.1
			if(_N!=0){
				keep delete_eabi
				duplicates drop delete_eabi, force
				ren delete_eabi abi
				gen year=`y'
				duplicates drop abi year, force
				save "MinDist_aucc_`y'.dta", replace
			}
		}
	}
}

use "RawUrgentCare_R3.dta", clear
keep if a_hospital==1
forvalues y=2005/2017{
	capture confirm file "MinDist_aucc_`y'.dta"
	  if _rc==0 {
		merge 1:1 abi year using "MinDist_aucc_`y'.dta"
		drop if _merge==3
		drop _merge
	  }		
}
drop if abi==.
drop if company==""
drop is_*
cap drop ucc
save "HospUrgentCare_R3.dta", replace

use "HospitalNames2015.dta", clear
merge m:1 zipcode using "ZipToZCTA.dta"
keep if _merge==3
drop _merge
merge m:1 zcta5a using "zcta_pcsa.dta"
keep if _merge==3
drop _merge
keep zipcode hsanum
duplicates drop
tempfile hosploc
save `hosploc'

use "HospUrgentCare_R3.dta", clear
cap drop geo_tag geo_tag2
merge m:1 zipcode using "ZipToZCTA.dta"
keep if _merge==3
drop _merge
merge m:1 zcta5a using "zcta_pcsa.dta"
keep if _merge==3
drop _merge
merge m:1 zipcode using `hosploc'
drop if _merge==2
egen max_merge = max(_merge), by(pcsa)
gen geo_tag = (max_merge==3)
gen geo_tag2 = (_merge==3)
drop _merge max_merge
drop zcta5a pcsa
save "HospUrgentCare_R3.dta", replace
	
* Append final datasets

use "UrgentCareMain_R3.dta", clear
cap drop ucc zcta5a todrop
gen ucc = 1
append using "HospUrgentCare_R3.dta", force
replace ucc = 0 if ucc==.
replace ucc = 1 if geo_tag==0 & geo_tag!=.
gen hospaff_ucc = 1 if ucc==0
replace hospaff_ucc = 0 if hospaff_ucc==.
tab year if ucc==1
save "UrgentCareAll_R3.dta", replace

use "UrgentCareAll_R3.dta", clear
merge m:1 zipcode using "uszips.dta"
drop if _merge==2
drop if state=="PR"
drop if state=="VI"
tab zipcode if _merge==1
drop _merge
merge m:1 zipcode using "ushrr.dta"
drop if _merge==2
drop _merge
merge m:1 zipcode using "ZipToZCTA.dta"
keep if _merge==3
drop _merge
save "UrgentCareAll_R3.dta", replace

save "UrgentCare_R3.dta", replace
